package servlet;

import pojo.BookPage;
import pojo.Category;
import pojo.Publisher;
import service.BookService;
import service.CategoryService;
import service.PublisherService;
import service.impl.BookServiceImpl;
import service.impl.CategoryServiceImpl;
import service.impl.PublisherServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Tiler on 2020/11/9
 */
public class IndexPage extends HttpServlet {
    private BookService bookService = new BookServiceImpl();

    private PublisherService publisherService = new PublisherServiceImpl();

    private CategoryService categoryService = new CategoryServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");

        Map<String, Object> params = new HashMap<>();

        String page = req.getParameter("page");
        String size = req.getParameter("size");
        String name = req.getParameter("name");
        String pstr = req.getParameter("pub");
        String[] cate = req.getParameterValues("cate");

        StringBuffer cids = null;

        if (cate != null && cate.length > 0) {
            cids = new StringBuffer();

            for (String s : cate) {
                if (cids.length() > 0) {
                    cids.append(",");
                }

                cids.append(s);
            }
        }

        Integer pid = (page==null)? 1 : Integer.parseInt(page);
        Integer num = (size==null)? 8 : Integer.parseInt(size);
        Integer pub = (pstr==null)? 0 : Integer.parseInt(pstr);

        params.put("page", pid);
        params.put("size", num);
        params.put("pub", pub);

        if (name != null && !name.equals("")) {
            params.put("name", name);
        }

        if (cids != null) {
            params.put("cate", cids.toString());
        }

        BookPage bookPage = bookService.getAllBooks(params);

        PublisherService publisherService = new PublisherServiceImpl();
        List<Publisher> publishers = publisherService.getAllPublishers();
        List<Category> categories = categoryService.getAllCategories();

        req.setAttribute("bookPage", bookPage);
        req.setAttribute("publishers", publishers);
        req.setAttribute("categories", categories);
        req.setAttribute("name", name);
        req.setAttribute("pub", pub);
        req.setAttribute("cate", cate);

        req.getRequestDispatcher("book_index.jsp").forward(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
